<?php
class OrderController extends BaseController
{
	public function _initialize() {
        parent::_initialize();
        $this->id = session('id');
    }
	public function index()
	{	
		//1：全部 2:交易中 3：已完成
		$type = !isset($_GET['type']) || $_GET['type'] == 1 ? 1 : $_GET['type'];
		// var_dump($type);
		if($type == 1){
			// var_dump(222);
			$order_info = M('hang_buy')->where(['user_id|sell_id'=>$this->id])->where(['status'=>['in',[1,2,3,4]]])->order('id desc')->select();
			if($order_info == null) $order_info = [];
			// var_dump($order_info);
			$sell_info = M('hang_sell')->where(['user_id'=>$this->id,'status'=>1])->select();
			// var_dump($sell_info);
			
			if($sell_info){
				foreach($sell_info as $key => $value){
					$sell_info[$key]['status'] = 0;
					$sell_info[$key]['user_id'] = 0;
					$sell_info[$key]['sell_id'] = $this->id;
					$sell_info[$key]['all_price'] = $value['left_price'];
				}
				$order_info = array_merge($order_info,$sell_info);
			}
			
			
		}elseif($type == 2){
			$order_info = M('hang_buy')->where(['user_id|sell_id'=>$this->id])->where(['status'=>['in',[1,2,3]]])->order('id desc')->select();
			$sell_info = M('hang_sell')->where(['user_id'=>$this->id,'status'=>1])->select();
			if($order_info == null) $order_info = [];
			// var_dump($sell_info);
			if($sell_info){
				foreach($sell_info as $key => $value){
					$sell_info[$key]['status'] = 0;
					$sell_info[$key]['user_id'] = 0;
					$sell_info[$key]['sell_id'] = $this->id;
					$sell_info[$key]['all_price'] = $value['left_price'];
				}
				$order_info = array_merge($order_info,$sell_info);
			}
			// var_dump($order_info);
		}elseif($type == 3){
			$order_info = M('hang_buy')->where(['user_id|sell_id'=>$this->id])->where(['status'=>4])->order('id desc')->select();
		}
		include template('/user/order/index');
	}

	public function order_detail()
	{	

		$order_id = $_GET['id'];
		
		$user_id = $this->id;
		
		$status = $_GET['status'];
		// var_dump($status);
	
		if($status != 0){
			$order_info = M('hang_buy')->where(['id'=>$order_id])->find();
			$timed = strtotime($order_info['update_time']);
			$config_time = C('site_hour')*60*60;
			$jia = $timed + $config_time;
			$order_info['time'] = $jia - time();
			if($order_info['user_id'] == $user_id){
				//说明是购买者
				if($order_info['status'] == 2){
					//待付款
					$info = $user_info = M('user')->where(['id'=>$order_info['sell_id']])->find();
					
					$info['number'] = $order_info['number'];
					$info['price'] = $order_info['price'];
					$info['all_price'] = $order_info['all_price'];
					$info['time'] = $order_info['update_time'];
					$bank_info = M('bank')->where(['user_id'=>$order_info['user_id'],'status'=>1])->find();
					include template('/user/order/buy_2');
				}elseif($order_info['status'] == 3){
					//待确认 
					$info = $user_info = M('user')->where(['id'=>$order_info['sell_id']])->find();
					$info['number'] = $order_info['number'];
					$info['price'] = $order_info['price'];
					$info['all_price'] = $order_info['all_price'];
					$info['time'] = $order_info['update_time'];
					$bank_info = M('bank')->where(['user_id'=>$order_info['user_id'],'status'=>1])->find();
					include template('/user/order/buy_3');
				}elseif($order_info['status'] == 4 && $order_info['buy_evaluate'] == 0){
					// echo 222;
					//已完成 未评价
					$info = $user_info = M('user')->where(['id'=>$order_info['sell_id']])->find();
					$info['number'] = $order_info['number'];
					$info['price'] = $order_info['price'];
					$info['all_price'] = $order_info['all_price'];
					$info['time'] = $order_info['update_time'];
					$bank_info = M('bank')->where(['user_id'=>$order_info['user_id'],'status'=>1])->find();
					//转账记录
					$transfer_info = M('transfer_records')
	
								->where(['order_id'=>$order_info['id']])
							
								->find();
					
					
					include template('/user/order/buy_4');
				}elseif($order_info['status'] == 4 && $order_info['buy_evaluate'] != 0){
					// echo 111;
					//已完成 未评价
					$info = $user_info = M('user')->where(['id'=>$order_info['sell_id']])->find();
					$info['number'] = $order_info['number'];
					$info['price'] = $order_info['price'];
					$info['all_price'] = $order_info['all_price'];
					$info['time'] = $order_info['update_time'];
					$bank_info = M('bank')->where(['user_id'=>$order_info['user_id'],'status'=>1])->find();
					//转账记录
					$transfer_info = M('transfer_records')
								->where(['order_id'=>$order_info['id']])
								->find();
					include template('/user/order/buy_5');
				}elseif($order_info['status'] == 1){
					//为匹配
					$info = $user_info = M('user')->where(['id'=>$order_info['user_id']])->find();
					$info['number'] = $order_info['number'];
					$info['price'] = $order_info['price'];
					$info['all_price'] = $order_info['all_price'];
					$info['time'] = $order_info['update_time'];
					// $bank_info = M('bank')->where(['user_id'=>$order_info['user_id'],'status'=>1])->find();
					//转账记录
					// $transfer_info = M('transfer_records')
	
					// 			->where(['order_id'=>$order_info['id']])
							
					// 			->find();
					include template('/user/order/buy_1');
				}
			}elseif($order_info['sell_id'] == $user_id){
				//说明是出售者
				if($order_info['status'] == 2){
					//待付款
					$info = $user_info = M('user')->where(['id'=>$order_info['user_id']])->find();
					$info['number'] = $order_info['number'];
					$info['price'] = $order_info['price'];
					$info['all_price'] = $order_info['all_price'];
					$info['time'] = $order_info['update_time'];
					// $bank_info = M('bank')->where(['user_id'=>$order_info['user_id'],'status'=>1])->find();
					include template('/user/order/sell_2');
				}elseif($order_info['status'] == 3){
					//待确认
					
					$info = $user_info = M('user')->where(['id'=>$order_info['user_id']])->find();
					$info['number'] = $order_info['number'];
					$info['price'] = $order_info['price'];
					$info['all_price'] = $order_info['all_price'];
					$info['time'] = $order_info['update_time'];
					// $bank_info = M('bank')->where(['user_id'=>$order_info['user_id'],'status'=>1])->find();
					//转账记录
					$transfer_info = M('transfer_records')
	
								->where(['order_id'=>$order_info['id']])
							
								->find();
					
					include template('/user/order/sell_3');
				}elseif($order_info['status'] == 4 && $order_info['sell_evaluate'] == 0){
					//已完成 wei评价
					
					$info = $user_info = M('user')->where(['id'=>$order_info['user_id']])->find();
					$info['number'] = $order_info['number'];
					$info['price'] = $order_info['price'];
					$info['all_price'] = $order_info['all_price'];
					$info['time'] = $order_info['update_time'];
					$bank_info = M('bank')->where(['user_id'=>$order_info['user_id'],'status'=>1])->find();
					//转账记录
					$transfer_info = M('transfer_records')
	
								->where(['order_id'=>$order_info['id']])
							
								->find();
					include template('/user/order/sell_4');
				}elseif($order_info['status'] == 4 && $order_info['sell_evaluate'] != 0){
					//已完成 wei评价
					
					$info = $user_info = M('user')->where(['id'=>$order_info['user_id']])->find();
					$info['number'] = $order_info['number'];
					$info['price'] = $order_info['price'];
					$info['all_price'] = $order_info['all_price'];
					$info['time'] = $order_info['update_time'];
					$bank_info = M('bank')->where(['user_id'=>$order_info['user_id'],'status'=>1])->find();
					//转账记录
					$transfer_info = M('transfer_records')
	
								->where(['order_id'=>$order_info['id']])
							
								->find();
					include template('/user/order/sell_5');
				}
			}
		}
		else{
			// var_dump(1111);
			$order_info = M('hang_sell')->where(['id'=>$order_id])->find();
			// var_dump($order_info);
			include template('/user/order/sell_1');
		}			
	}

	


	public function upd_trade_status()
	{
		if(IS_POST){
			$order_id = $_POST['id'];
			$order_info = M('hang_buy')->where(['id'=>$order_id])->find();
			$passowrd = trim($_POST['password']);
			$user_info = M('user')->where(['id'=>$this->id])->field('id,username,real_name,salt,pay_password')->find();
			if(md5($user_info['salt'].$passowrd) != $user_info['pay_password']) showmessage('支付密码错误');
			try{
				$data = [
					'status'=>3,
					'update_time'=>date('Y-m-d H:i:s')
				];
				M('hang_buy')->where(['id'=>$order_id])->save($data);
				//生成支付记录
				$data1= [
					'user_id'=>$order_info['user_id'],
					'order_id'=>$order_id,
					'sell_id'=>$order_info['sell_id'],
					'order_number'=>time(),
					'status'=>1,
					'add_time'=>date('Y-m-d H:i:s'),
					'remarks'=>'购买'.$order_info['number'].'GF',
				];
				m('transfer_records')->add($data1);
			}catch(\Exception $e){
				showmessage($e->getMessage());
			}
			showmessage('操作成功，请及时上传交易凭证','goback',1);	
		}else{
			showmessage('非法访问');
		}
	}


	//举报

	public function jubao()
	{
		if(IS_POST){
			$param = I('post.');
			
			if($param['type'] == 'buy'){
				$result = M('hang_buy')->where(['id'=>$param['id']])->setField('buy_content',$param['buy_content']);
				if(!$result) showmessage('操作失败');
			}else{
				$result = M('hang_buy')->where(['id'=>$param['id']])->setField('sell_content',$param['sell_content']);
				if(!$result) showmessage('操作失败');
			}
			showmessage('操作成功','goback',1);
		}else{
			showmessage('非法访问');
		}
	}

	public function evaluate(){
		$param = I('post.');
		if($param['type'] == 'buy'){
			$data = [
				'buy_evaluate'=>$apram['value'],
			];
			$result = M('hang_buy')->where(['id'=>$param['id']])->setField('buy_evaluate',$param['value']);
		}else{
			$result = M('hang_buy')->where(['id'=>$param['id']])->setField('sell_evaluate',$param['value']);
		}
		if($result){
			showmessage('操作成功','goback',1);
		}else{
			showmessage('操作失败');
		}
		
	}


	//确认收款
	public function sure_get_money()
	{
		if(IS_POST){
			$id = $_POST['id'];
			$order_info =M('hang_buy')->where(['id'=>$id])->find();
			try{
				M()->startTrans();
				$data = [
					'status'=>4,
					'update_time'=>date('Y-m-d H:i:s')
				];
				M('hang_buy')->where(['id'=>$id])->save($data);
				M('transfer_records')->where(['order_id'=>$id])->setField('status',2);
				//返还冻结金
				$user_info  = M('user')->where(['id'=>$order_info['user_id']])->field('id,balance,freeze_balance')->find();
				$user_data = [
					'balance'=> $user_info['balance'] + 100,
					'freeze_balance'=> ($user_info['freeze_balance'] - 100) ? ($user_info['freeze_balance'] - 100) : 0,
				];
				M('user')->where(['id'=>$order_info['user_id']])->save($user_data);
				
				$user1_data = [
					'user_id'=>$user_info['id'],
					'balance'=>100,
					'type'=>1,
					'remarks'=>'交易成功，余额返还100',
					'add_time'=>date('Y-m-d H:i:s')
				];
				M('balance_exchange_records')->add($user1_data);
				M()->commit();
			}catch(\Exception $e){
				M()->rollback();
				showmessage($e->getMessage());
			}
			showmessage('操作成功','goback',1);
		}else{
			showmessage('非法访问');
		}
	}


	//取消购买
	public function cancel_buy(){
		if(IS_POST){
			$order_id = $_POST['id'];
			$order_info = M('hang_buy')->where(['id'=>$order_id])->find();
			$passowrd = trim($_POST['password']);
			$user_info = M('user')->where(['id'=>$this->id])->find();
			if(md5($user_info['salt'].$passowrd) != $user_info['pay_password']) showmessage('支付密码错误');


			try{

				M()->startTrans();
				//归还交易保证金，改变交易状态，生成余额变动记录
				$data = [
					'balance'=>$user_info['balance'] + 100,
					'freeze_balance'=> $user_info['freeze_balance'] - 100
				];
				M('user')->where(['id'=>$this->id])->save($data);
				$data1 = [
					'status'=>7,
					'update_time'=>date('Y-m-d H:i:s'),
				];
				M('hang_buy')->where(['id'=>$order_id])->save($data1);
				$data2 = [
					'user_id'=>$this->id,
					'balance'=>100,
					'type'=>3,
					'remarks'=>'取消购买',
					'add_time'=>date('Y-m-d H:i:s'),
				];
				M('balance_exchange_records')->add($data2);
				M()->commit();
			}catch(\Exception $e){
				M()->rollback();
				showmessage($e->getMessage());
			}
			showmessage('取消成功','goback',1);	
		}else{
			showmessage('非法访问');
		}
	}

	//取消出售
	public function cancel_sell(){
		if(IS_POST){
			$order_id = $_POST['id'];
			$order_info = M('hang_sell')->where(['id'=>$order_id])->find();
			$passowrd = trim($_POST['password']);
			$user_info = M('user')->where(['id'=>$this->id])->find();
			if(md5($user_info['salt'].$passowrd) != $user_info['pay_password']) showmessage('支付密码错误');


			try{

				M()->startTrans();
				$shouxu = ($order_info['charge_percent']/100+1)*$order_info['number'];
				//归还交易保证金，改变交易状态，生成余额变动记录
				$data = [
					'balance'=>$user_info['balance'] + 100,
					'freeze_balance'=> $user_info['freeze_balance'] - 100,
					'upvaluation'=>$user_info['upvaluation'] + $shouxu,
					'freeze_upvaluation'=>$user_info['freeze_upvaluation']-$shouxu
				];
				M('user')->where(['id'=>$this->id])->save($data);
				// $data1 = [
				// 	'status'=>7,
				// 	'update_time'=>date('Y-m-d H:i:s'),
				// ];
				// M('hang_sell')->where(['id'=>$order_id])->save($data1);
				M('hang_sell')->where(['id'=>$order_id])->setField('status',0);
				$data2 = [
					'user_id'=>$this->id,
					'balance'=>100,
					'type'=>3,
					'remarks'=>'取消卖出',
					'add_time'=>date('Y-m-d H:i:s'),
				];
				$data3 = [
					'user_id'=>$this->id,
					'balance'=>$shouxu,
					'type'=>3,
					'remarks'=>'取消卖出',
					'add_time'=>date('Y-m-d H:i:s'),

				];
				M('upvaluation_exchange_records')->add($data3);
				M('balance_exchange_records')->add($data2);
				M()->commit();
			}catch(\Exception $e){
				M()->rollback();
				showmessage($e->getMessage());
			}
			showmessage('取消成功','goback',1);	
		}else{
			showmessage('非法访问');
		}
	}
}